﻿using IServices;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using YL.Core.Dto;
using YL.Core.Entity;
using YL.Core.Entity.Fluent.Validation;
using YL.NetCore.Attributes;
using YL.NetCore.NetCoreApp;
using YL.Utils.Extensions;
using YL.Utils.Pub;
using YL.Utils.Table;

namespace Method.SmartMeter.Controllers
{
    public class ClientInfoController : BaseController
    {
        private readonly IMeter_ClientInfoServices _clientInfoServices;
        private readonly ISys_deptServices _deptServices;

        public ClientInfoController(IMeter_ClientInfoServices clientInfoServices, ISys_deptServices deptServices)
        {
            _clientInfoServices = clientInfoServices;
            _deptServices = deptServices;
        }

        [HttpGet]
        [CheckMenu]
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [OperationLog(LogType = LogType.select)]
        public ContentResult List([FromForm]Bootstrap.BootstrapParams bootstrap)
        {
            bootstrap.depts = this.UserDepts;
            var sd = _clientInfoServices.PageList(bootstrap);
            return Content(sd);
        }

        [HttpPost]
        [OperationLog(LogType = LogType.select)]
        public ContentResult SingleList()
        {
            var sd = _clientInfoServices.SingleList(this.UserDepts);
            return Content(sd);
        }


        [HttpGet]
        public IActionResult Add(string id)
        {
            ClientInfo client = new ClientInfo();
            if (id.IsEmpty())
            {
                List<Sys_dept> list = new List<Sys_dept>();
                ViewBag.deptResult = JsonConvert.SerializeObject(list);
                return View(client);
            }
            else
            {

                client = _clientInfoServices.QueryableToEntity(c => c.ClientId == SqlFunc.ToInt64(id));
                var dept = _deptServices.QueryableToEntity(c => c.DeptId == client.ParentId);
                ViewBag.DeptNo = dept.DeptNo;
                ViewBag.DeptName = dept.DeptName;
                string deptList = _deptServices.SingleList("", UserDtoCache.UserId);

                ViewBag.deptResult = JsonConvert.SerializeObject(deptList);
                return View(client);
            }
        }

        [HttpPost]
        [FilterXss]
        [OperationLog(LogType = LogType.addOrUpdate)]
        public IActionResult AddOrUpdate([FromForm]ClientInfo clientInfo, [FromForm]string id)
        {
            var validator = new MeterClientInfoFluent();
            var results = validator.Validate(clientInfo);
            var success = results.IsValid;
            if (!success)
            {
                string msg = results.Errors.Aggregate("", (current, item) => (item.ErrorMessage + "</br>"));
                return BootJsonH((PubEnum.Failed.ToInt64(), msg));
            }
            if (clientInfo.ParentId > 0)
            {
                clientInfo.ClientArea = _deptServices.GetTheData(clientInfo.ParentId)?.DeptName;
            }
            if (id.IsEmptyZero())
            {
                //同一层级下不允许有两个相同的客户
                if (_clientInfoServices.IsAny(c => c.ClientName == clientInfo.ClientName && c.ParentId==clientInfo.ParentId))
                {
                    return BootJsonH((false, PubConst.Dept2));
                }

                clientInfo.UserId = UserDtoCache.UserId;//获取当前的用户id
                bool flag = _clientInfoServices.Insert(clientInfo);
                return BootJsonH(flag ? (flag, PubConst.Add1) : (flag, PubConst.Add2));
            }
            else
            {
                clientInfo.ClientId = id.ToInt64();
                clientInfo.ModifiedBy = UserDtoCache.UserId;
                var flag = _clientInfoServices.Update(clientInfo);
                return BootJsonH(flag ? (flag, PubConst.Update1) : (flag, PubConst.Update2));
            }
        }

        [HttpGet]
        [OperationLog(LogType.delete)]
        public IActionResult Delete(string id)
        {
            var flag = _clientInfoServices.Delete(x=>x.ClientId== id.ToInt64());
            return BootJsonH(flag ? (flag, PubConst.Delete1) : (flag, PubConst.Delete2));
        }
    }
}